home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / cut_past.zip / SPASTE.C < prev   
Text File  |  1987-03-04  |  3KB  |  122 lines

  1. /*
  2.  * Serially paste a file together
  3.  *
  4.  * John Weald
  5.  */
  6. #include <stdio.h>
  7.  
  8. #define    BUFSIZ 512
  9.  
  10. extern exit();
  11.  
  12.  
  13. spaste(files, c, n)
  14. char *files[];        /* Null terminate list of input files        */
  15. char c[];        /* The concatintaion characters            */
  16. int n;            /* The number of above                */
  17. {
  18.     int i;
  19.     FILE *fp;
  20.  
  21.     if (files[0] == NULL)
  22.     {
  23.         spfile(stdin, c, n);
  24.         return;
  25.     }
  26.  
  27.     for (i = 0; files[i] != NULL; i++)
  28.     {
  29.         if (*files[i] == '-')
  30.             fp = stdin;
  31.         else if ((fp = fopen(files[i], "r")) == (FILE *)NULL)
  32.         {
  33.             fprintf(stderr, "Failed to open file %s\n", files[i]);
  34.             exit(1);
  35.         }
  36.         spfile(fp, c, n);
  37.         fclose(fp);
  38.     }
  39. }
  40.     
  41. /* 
  42.  * Do the actual paste of a stream.
  43.  *
  44.  * The method here is to read in the stream and replace all newline
  45.  * characters with concatintaion characters. 
  46.  * Output occurs after each chuck is parsed, or if the concatination character
  47.  * is the null seperator (otherwise puts() would screw up on whole chunk).
  48.  *
  49.  * The stream is read in BUFSIZ chunks using fread. The input buffer is one
  50.  * larger than read, so that it can be null terminated. 
  51.  *
  52.  * When we read in each chunk we must check if it needs to be joined to the
  53.  * previous one i.e. the last character on the last chunk was a newline.
  54.  */
  55. static
  56. spfile(fp, con, ncons)
  57. FILE *fp;        /* serially paste this stream            */
  58. char con[];        /* The concatintaion characters            */
  59. int ncons;        /* The number of above                */
  60. {
  61.     char *pstart;    /* The start of the string            */
  62.     char *ptr;    /* Walks down the stream            */
  63.     char buf[BUFSIZ + 1]; /* To ensure null termination        */
  64.     int n;        /* Number of bytes read with fread()        */
  65.     int k = 0;    /* Index into concatination character array    */
  66.     int join;    /* Join this chunk to the next?            */
  67.     char last;    /* The very last character looked at.        */
  68.  
  69.  
  70.     join = 0;
  71.     while ((n = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) != 0)
  72.     {
  73.         if (join)
  74.         {
  75.             /* Join with last chunk */
  76.             putchar(con[k]);
  77.             k = (k + 1) % ncons;
  78.             join = 0;
  79.         }
  80.         /* Join to next chunk? */
  81.         if (buf[n-1] == '\n')
  82.         {
  83.             join++;
  84.             /* Ignore the newline */
  85.             n--;
  86.         }
  87.  
  88.         /* ensure null terminated buffer */
  89.         buf[n] = '\0';
  90.  
  91.         
  92.         /* 
  93.           * walk thru this chunk 
  94.          * replace all newlines with the next concat. char.
  95.          */
  96.         for (pstart = ptr = buf; *ptr != '\0'; ptr++)
  97.         {
  98.             if (*ptr == '\n')
  99.             {
  100.                 *ptr = con[k];
  101.                 if (con[k] == '\0')
  102.                 {
  103.                     fputs(pstart, stdout);
  104.                     pstart = ptr + 1;
  105.                 }
  106.                 k = (k + 1) % ncons;
  107.             }
  108.         }
  109.         fputs(pstart, stdout);
  110.  
  111.         last = *(ptr - 1);
  112.     }
  113.  
  114.     /*
  115.      * Maybe they asked for the newline as the
  116.      * concatination char. We would hate to give them two newlines
  117.      * in a row.
  118.      */
  119.     if (last != '\n')
  120.         putchar('\n');
  121. }
  122.